package com.sf.fy.uav.applet.config;

import com.sfpay.sdk.config.PrivateKeyParams;
import com.sfpay.sdk.config.SFPayConfig;
import com.sfpay.sdk.config.SFPayRegister;
import com.sfpay.sdk.config.SimpleSelector;
import jakarta.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author LTQ
 */
@Component
public class SFPayGlobalConfig {
    @Value("${pay.key-path:D:\\\\}")
    private String payKeyPath;

    @Value("${pay.sit-url: http://sfpay-apis.intsit.sfcloud.local:1080}")
    private String sitUrl;

    @Value("${pay.mchId: 2000146721}")
    private String mchId;


    @Value("${pay.private-key-password: 123456}")
    private String privateKeyPassword;


    @Value("${pay.secretKey: 30d458730527ad9d75a20a27e035e843}")
    private String secretKey;





    private final static Logger logger = LoggerFactory.getLogger(SFPayGlobalConfig.class);

    @PostConstruct
    public void init() {
        // 顺手付初始化配置对象
        SFPayConfig sfPayConfig = new SFPayConfig();

        // 设置默认商户号 非必填 ** 设置默认商户号, 发起请求时可无需指定商户号
        sfPayConfig.setDefaultMchId(mchId);

        sfPayConfig.setDomain(sitUrl);


        /*
            必填, 证书及秘钥获取器 ** 由商户自定义获取逻辑!!!
            !! 注意：实际开发过程中，请务必注意不要将私钥证书信息, 加密秘钥配置在源码中（比如配置为常量或储存在配置文件的某个字段中等），因为私钥的保密等级往往比源码高得多，将会增加私钥泄露的风险。
            !! 此处为了example执行的环境普适性，私钥文件配置在resources资源下，实际过程中请不要这样做。
         */
        sfPayConfig.setSelector(new SimpleSelector() {

            @Override
            public String publicKeyPath(String signType, String sfpayCertSn) {
                /*
                    顺手付公钥证书获取
                    signType: rsa
                    sfpayCertSn: 32bc3e07730b10aeeb86dad620bda840cc7ac069
                 */
                return payKeyPath + "/" + "sfpay_public" + ".cer";
            }

            @Override
            public PrivateKeyParams privateKey(String signType, String mchId) {
                /*
                    商户私钥获取
                    signType: rsa
                    mchId: 10000000500
                 */
                logger.info("payKeyPath:{},mchId:{}", payKeyPath, mchId);
                String path = payKeyPath + "/" + mchId + ".pfx";
                return new PrivateKeyParams(path, privateKeyPassword);
            }

            // 仅用于测试演示
            private final Map<String, String> KEY_MAP = new HashMap<String, String>() {{
                put(mchId + "_aes", secretKey);
            }};

            @Override
            public String encryptKey(String encryptType, String mchId) {
                // 商户加密私钥获取
                /*
                    encryptType: aes
                    mchId: 10000000500
                 */
                return KEY_MAP.get(mchId + "_" + encryptType);
            }
        });

        // 初始化配置
        SFPayRegister.init(sfPayConfig);
    }

}
